<?php
/**
 * Created by PhpStorm.
 * User: 胡诚
 * Date: 2018/11/9
 * Time: 16:30
 */

namespace App\Services;


use App\Models\AdminUser;
use App\Models\Hall;
use App\Models\HallConfig;
use App\Models\SmsConfig;
use common\enum\CommonEnum;
use common\enum\MasterLogEnum;
use common\utils\SysCode;

class HallSettingService
{

    /**
     * 修改厅主状态
     * @param array $values
     * @return mixed
     * @throws \App\Exceptions\CustomException
     */
    public static function updateStaus(array $values)
    {
        $hallName         = Hall::where(['id' => $values['hall_id']])->first()->toArray();
        $hallConfig       = HallConfig::where(['hall_id' => $values['hall_id']])->first()->toArray();
        $update['status'] = $values['hall_status'];
        if ($values['hall_status'] == CommonEnum::DELETE) {
            if (isset($values['admin_status']) || isset($values['user_status'])) {
                $update['is_admin_freeze'] = $values['admin_status'] ?? CommonEnum::DISABLE;
                $update['is_user_freeze']  = $values['user_status'] ?? CommonEnum::DISABLE;
            } else {
                error_exit(SysCode::INVALID_ARGUMENT);
            }
        } elseif ($values['hall_status'] == CommonEnum::ENABLED) {
            $update['is_admin_freeze'] = CommonEnum::ENABLED;
            $update['is_user_freeze']  = CommonEnum::ENABLED;
        }
        $result = HallConfig::where(['hall_id' => $values['hall_id']])->update($update);
        if (!$result) {
            error_exit(SysCode::FAILED);
        }
        $remark = str_replace(['{:name}', '{:old}', '{:new}'], [$hallName['company_name'], MasterLogEnum::STATUS[$hallConfig['status']], MasterLogEnum::STATUS[$update['status']]], MasterLogEnum::REMARK['hall_status_edit']);
        MasterLogService::writeMasterLog(MasterLogEnum::HALL_STATUS_EDIT, MasterLogEnum::SUCCESS, $remark);
        return $result;
    }

    /**
     * 修改厅主密码
     * @param array $value
     * @return mixed
     * @throws \App\Exceptions\CustomException
     */
    public static function updatePassword(array $value)
    {
        if ($value['password_one'] !== $value['password_two']) {
            error_exit(SysCode::HALL_SETTING_PWD_ERROR);
        }
        $update['salt']     = salt();
        $update['password'] = password_encrypt($value['password_one'], $update['salt']);

        $userId   = Hall::where(['id' => $value['hall_id']])->value('admin_uid');
        $userData = AdminUser::where(['id' => $userId])->first();
        if (!$userData) {
            error_exit(SysCode::HALL_SETTING_USER_ERROR);
        }
        $result = AdminUser::where(['id' => $userId])->update($update);
        if (!$result) {
            error_exit(SysCode::FAILED);
        }
        return $result;
    }

    /**
     * 获取短信平台
     * @param int $hallId
     * @return mixed
     */
    public static function getSmsPlatform(int $hallId)
    {
        $select  = ['id as sms_id', 'name', 'config', 'status'];
        $data    = SmsConfig::select($select)->where(['hall_id' => $hallId])->get()->toArray();
        $smsData = [];
        foreach ($data as $value) {
            $value['config'] = json_decode($value['config'], true);
            $smsData[]       = $value;
        }
        return $smsData;
    }

    /**
     * 修改短信数据
     * @param array $value
     * @return mixed
     * @throws \App\Exceptions\CustomException
     */
    public static function updateSms(array $value)
    {
        $where  = ['hall_id' => $value['hall_id'], 'status' => CommonEnum::ENABLED];
        $result = SmsConfig::where($where)->update(['status' => CommonEnum::DISABLE]);
        if (!$result) {
            error_exit(SysCode::HALL_SETTING_SMS_ERROR);
        }
        $update = [
            'config'     => json_encode($value['config'], JSON_UNESCAPED_UNICODE),
            'status'     => CommonEnum::ENABLED,
        ];
        $res    = SmsConfig::where(['id' => $value['sms_id']])->update($update);
        if (!$res){
            error_exit(SysCode::HALL_SETTING_SMS_ERROR);
        }
        return $res;
    }

    /**
     * 获取用户列表
     * @param int $hallId
     * @return mixed
     */
    public static function googleList(int $hallId)
    {
        $userData = AdminUser::select(['username', 'google_auth_key as key', 'id as user_id'])->where(['hall_id' => $hallId])->get()->toArray();
        $data     = [];
        foreach ($userData as $key => $val) {
            $data[$key] = $val;
            if (!empty($val['key'])) {
                $data[$key]['status'] = CommonEnum::ENABLED;
            } else {
                $data[$key]['status'] = CommonEnum::DISABLE;
            }
            unset($data[$key]['key']);
        }
        return $data;
    }

    /**
     * 修改Google验证绑定状态,解绑密钥为空
     * @param array $values
     * @return mixed
     */
    public static function updateGoogleStatus(array $values)
    {
        return AdminUser::where(['id' => $values['user_id']])->update(['google_auth_key' => CommonEnum::DISABLE]);
    }

}